package com.ougy.reshop.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.ougy.reshop.dao.IUserDao;
import com.ougy.reshop.model.User;

@Repository
public class UserDao extends BaseDao<User> implements IUserDao {
	private static final Logger log = LoggerFactory.getLogger(UserDao.class);

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from User as u where u." + propertyName
					+ "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public User findByUsername(Object username) {
		List list = findByProperty("username", username);
		if (list.size() == 0) {
			return null;
		}
		return (User) list.get(0);
	}



	@Override
	public User findByUsername(String username) {
		List list = findByProperty("username",username);
		if (list.size() == 0) {
			return null;
		}
		return (User) list.get(0);
	}

}
